package com.nand.module.function.ui;

import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextClock;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.Callback;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.base.utils.ObjectUtils;
import com.nand.library.base.utils.SpUtils;
import com.nand.module.function.R;
import com.nand.module.function.adapter.PatrolPageAdapter;
import com.nand.module.function.bean.EqDatailBean;
import com.nand.module.function.bean.EqPollingBean;
import com.nand.module.function.bean.IdBean;
import com.nand.module.function.bean.ManageBean;
import com.nand.module.function.bean.PollingBean;
import com.nand.module.function.constant.URL;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.view.CommonPopupWindow;
import com.nand.module.function.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * updated by wzhao on 2020/3/26
 * describe:点巡检页面
 */
public class PatrolPageActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvHeadline;

    private LinearLayout llCkd,llSbd;
    private TextView tvCkd,tvSbd, mTvJclx,mTvSbbh,mTvSbmc,mTvSybm;

    private CommonPopupWindow windowContent;
    ArrayList<String> jclxList = new ArrayList<>();
    private WheelView mWheelview;
    private TextView mTvCancel;
    private String item = "";
    private String equipBelong;

    private CommonPopupWindow.LayoutGravity layoutGravity;

    private RecyclerView mRecycler;
    private PatrolPageAdapter mRecyclerAdapter;

    private List<PollingBean> mList = new ArrayList<>();
    private int dxjID = 0;
    private int dxjgID = 1;
    private int jclxID = 2;
    private int type;
    private ImageView ivRight3;
    private String checkID;
    private TextView mTvConfirm1;
    private String id;
    private String eqID;
    private String tabJson = "";
    private TextView horiText;
    private String userName;
    private int userId;
    private TextView mTvXj;
    private TextView mTvDj;
    private TextView mTvBhg,mTvHg,mTvTj;

    @Override
    protected int initLayout() {
        return R.layout.activity_patrol_page;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type", 1); //1.详情  2.添加
        id = getIntent().getStringExtra("id");  //设备ID
        eqID = getIntent().getStringExtra("eqID");  //设备编码ID
        userId = SpUtils.readInt(PatrolPageActivity.this, "userId", 0);
        userName = SpUtils.readString(PatrolPageActivity.this, "userName", "");

        tvHeadline = findViewById(R.id.tv_headline);
        tvSbd = findViewById(R.id.tv_sbd);
        tvSbd.setOnClickListener(this);
        tvCkd = findViewById(R.id.tv_ckd);
        tvCkd.setOnClickListener(this);
        llSbd = findViewById(R.id.ll_sbd);
        llCkd = findViewById(R.id.ll_ckd);
        mTvSbbh = findViewById(R.id.tv_sbbh);
        mTvSbmc = findViewById(R.id.tv_sbmc);
        mTvSybm = findViewById(R.id.tv_sybm);

        mTvDj = findViewById(R.id.tv_dj);
        mTvXj = findViewById(R.id.tv_xj);
        mTvHg = findViewById(R.id.tv_hg);
        mTvBhg = findViewById(R.id.tv_bhg);
        mTvTj = findViewById(R.id.tv_tj);
        mTvJclx = findViewById(R.id.tv_jclx);
        ivRight3 = findViewById(R.id.iv_right_arrow3);

        mRecycler = findViewById(R.id.recyclerview);
        mTvConfirm1 = findViewById(R.id.tv_confirm);
        mTvConfirm1.setOnClickListener(this);
    }
    private String[] jclxStrting = new String[]{"机械", "电气"};
    private void iniktAddBean() {
        for (int i = 0; i < jclxStrting.length; i++) {
            jclxList.add(jclxStrting[i]);
        }
    }
    private OptionsPickerView jclxPicker;
    @Override
    protected void initData() {
        jclxPicker = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                jclxID = options1;
                mTvJclx.setText(jclxList.get(options1));
            }
        }).setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setSubCalSize(14)//确定和取消文字大小
                .setSubmitColor(Color.GRAY)//确定按钮文字颜色
                .setCancelColor(Color.GRAY)//取消按钮文字颜色
                .setContentTextSize(18)//滚轮文字大小
                .setCyclic(false, false, false)//循环与否
                .build();

        initPop();
        switch (type) {
            case 1: //详情
                tvHeadline.setText("设备/巡检详情");
                mTvConfirm1.setVisibility(View.GONE);
                ivRight3.setVisibility(View.GONE);
                LogUtils.e("daaata========="+id);
                HttpParams params = new HttpParams();
                params.put("id", id);
                OkGoUtils.getInstance().post(this, URL.EQ_CHECK_LIST, params, 2, true, callback);
                break;
            case 2: //新增
                tvHeadline.setText("设备/巡检新增");
                iniktAddBean();
                mTvHg.setOnClickListener(this);
                mTvBhg.setOnClickListener(this);
                mTvTj.setOnClickListener(this);
                mTvDj.setOnClickListener(this);
                mTvXj.setOnClickListener(this);
                initPop();
                findViewById(R.id.ll_jclx).setOnClickListener(PatrolPageActivity.this);
                HttpParams params1 = new HttpParams();
                params1.put("id", eqID);
                OkGoUtils.getInstance().post(this, URL.EQ_DETAIL_LIST, params1, 1, true, callback);
                break;

        }
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerAdapter = new PatrolPageAdapter(PatrolPageActivity.this);
      //  mRecycler.addItemDecoration(new SpacesItemDec oration(this, 18, R.color.color_f4));
        mRecycler.setAdapter(mRecyclerAdapter);

        mRecyclerAdapter.setOnItemClickListener(new PatrolPageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onJcnrClick(int position, String context, TextView textview) {
                layoutGravity = new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.ALIGN_LEFT | CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                layoutGravity.setHoriGravity(CommonPopupWindow.LayoutGravity.ALIGN_RIGHT);
                layoutGravity.setVertGravity(CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                windowContent.showBashOfAnchor(textview, layoutGravity, 0, 0);
                horiText.setText(context);
            }
        });
    }

    private String ab = "";
    private String equipID;

    private MyCallback callback = new MyCallback() {
        @Override
        public void onSuccess(ResponseBean response, int id) {
            if (response.getCode() == 200) {
                String data = response.getDataInfo().toString();
                LogUtils.e("data==========" + data);
                switch (id){
                    case 1: //新增设备信息
                        EqDatailBean beanF = JSON.parseObject(data, EqDatailBean.class);
                        if (beanF.getDataList().size() > 0) {
                            EqDatailBean eqDatailBean = JSON.parseObject(data, EqDatailBean.class);
                            EqDatailBean.DataListBean bean = eqDatailBean.getDataList().get(0);

                            equipID = bean.getId() + "";
                            mTvSbbh.setText(bean.getEquipID());
                            mTvSbmc.setText(bean.getEquipName());
                            mTvSybm.setText(bean.getUseDepartID());
                            equipBelong = bean.getfBelongOid();
                        } else {
                            showToast("暂无数据");
                        }
                        break;
                    case 2:
                        EqDatailBean datailBean = JSON.parseObject(data, EqDatailBean.class);
                        if (datailBean.getDataList().size() > 0) {
                            EqDatailBean.DataListBean bean = datailBean.getDataList().get(0);
                            equipID = bean.getEquipID();
                            checkID = bean.getCheckID();
                            mTvSbbh.setText(bean.getEquipID());
                            mTvSbmc.setText(bean.getEquipName());
                            mTvSybm.setText(bean.getUseDepart());

                            int isPobigint = bean.getIsPobigint();
                            if (0 == isPobigint) {
                                initShowTab(mTvDj,mTvXj,null);
                            } else {
                                initShowTab(mTvXj,mTvDj,null);
                            }

                            int checkResult = bean.getCheckResult();
                            switch (checkResult){
                                case 0:  //不合格
                                    initShowTab(mTvBhg,mTvHg,mTvTj);
                                    break;
                                case 1:  //合格
                                    initShowTab(mTvHg,mTvBhg,mTvTj);
                                    break;
                                case 2:  //停机
                                    initShowTab(mTvTj,mTvHg,mTvBhg);
                                    break;
                            }


                            int checkType = bean.getCheckType();
                            if (0 == checkType) {
                                mTvJclx.setText("机械");
                            } else {
                                mTvJclx.setText("电器");
                            }

                            equipBelong = bean.getEquipBelong();

                        } else {
                            showToast("暂无数据");
                        }
                        break;
                    case 3: //检查项详情
                        EqPollingBean listBeans = JSON.parseObject(data, EqPollingBean.class);
                        List<PollingBean> dataList = listBeans.getDataList();

                        mList.clear();
                        for (PollingBean bean : dataList) {
                            mList.add(bean);
                        }
                        mRecyclerAdapter.setData(mList, 1);
                        break;
                    case 4:  //检查项数据
                        EqPollingBean pollingBea = JSON.parseObject(data, EqPollingBean.class);

                        mList.clear();
                        if (pollingBea.getDataList().size() != 0) {
                            List<PollingBean> dataList4 = pollingBea.getDataList();
                            for (PollingBean bean : dataList4) {
                                mList.add(bean);
                            }
                            mRecyclerAdapter.setData(mList, 2);
                        }else {
                            mRecyclerAdapter.setData(null,2);
                        }
                        break;
                }

            } else {
                showToast(response.getMsg());
            }
        }

        @Override
        public void onError(Throwable e, int id) {
        }
    };
    private void initPop() {
        windowContent = new CommonPopupWindow(this, R.layout.item_show_content, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
            @Override
            protected void initView() {
                View view = getContentView();
                horiText = view.findViewById(R.id.hori_text);
            }
            @Override
            protected void initEvent() {
            }
        };
    }

    private int confirmType = 1;
    private int whetherCheckItem = 0; //是否选择检查项 0,未选择 1、已选择
    private boolean isFirst = true;
    private int isOkId = 2;

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_sbd) {
            confirmType = 1;
            findViewById(R.id.tv_sbd).setBackgroundColor(getResources().getColor(R.color.color_1a0077f7));
            findViewById(R.id.tv_ckd).setBackgroundColor(getResources().getColor(R.color.white));
            llSbd.setVisibility(View.VISIBLE);
            llCkd.setVisibility(View.GONE);
            tvSbd.setTextColor(getResources().getColor(R.color.color_33));
            tvCkd.setTextColor(getResources().getColor(R.color.color_99));
        } else if (id == R.id.tv_ckd) {

            switch (type) {
                case 1: //详情
                    findViewById(R.id.tv_ckd).setBackgroundColor(getResources().getColor(R.color.color_1a0077f7));
                    findViewById(R.id.tv_sbd).setBackgroundColor(getResources().getColor(R.color.white));
                    llCkd.setVisibility(View.VISIBLE);
                    llSbd.setVisibility(View.GONE);
                    tvSbd.setTextColor(getResources().getColor(R.color.color_99));
                    tvCkd.setTextColor(getResources().getColor(R.color.color_33));
                    HttpParams params1 = new HttpParams();
                    params1.put("checkID", checkID);
                    OkGoUtils.getInstance().post(this, URL.EQ_CHECK_DETAIL, params1, 3, false, callback);
                    break;
                case 2:
                    confirmType = 2;
                    findViewById(R.id.tv_ckd).setBackgroundColor(getResources().getColor(R.color.color_1a0077f7));
                    findViewById(R.id.tv_sbd).setBackgroundColor(getResources().getColor(R.color.white));
                    llCkd.setVisibility(View.VISIBLE);
                    llSbd.setVisibility(View.GONE);
                    tvSbd.setTextColor(getResources().getColor(R.color.color_99));
                    tvCkd.setTextColor(getResources().getColor(R.color.color_33));

                    if (dxjID != isOkId) {
                        isOkId = dxjID;
                        whetherCheckItem = 0;
                        HttpParams params = new HttpParams();
                        params.put("equipBelong", equipBelong);
                        params.put("isPoint", dxjID);  //0点；1巡
                        OkGoUtils.getInstance().post(this, URL.EQ_MM_CHECK_STAND, params, 4, false, callback);
                        isFirst = false;
                    }
                    break;
            }


        } else if (id == R.id.tv_dj) {
            dxjID = 0;
            initShowTab(mTvDj,mTvXj,null);
        } else if (id == R.id.tv_xj) {
            dxjID = 1;
            initShowTab(mTvXj,mTvDj,null);
        } else if (id == R.id.tv_hg) {
            dxjgID = 1;
            initShowTab(mTvHg,mTvBhg,mTvTj);
        } else if (id == R.id.tv_bhg) {
            dxjgID = 0;
            initShowTab(mTvBhg,mTvHg,mTvTj);
        }  else if (id == R.id.tv_tj) {
            dxjgID = 2;
            initShowTab(mTvTj,mTvHg,mTvBhg);
        } else if (id == R.id.ll_jclx) {  /**检查类型选择*/
            if (ObjectUtils.notEmpty(jclxList)) {
                jclxPicker.setPicker(jclxList);
                jclxPicker.show();
            }
        } else if (id == R.id.tv_confirm) {

            if (2 == confirmType) {
                List<PollingBean> pollingBean = mRecyclerAdapter.getPollingBean();
                if (pollingBean == null){
                    showToast("请选择有检查项的设备");
                    return;
                }
                for (int i = 0; i < pollingBean.size(); i++) {
                    int result = pollingBean.get(i).getResult();
                    if (0 == result || 1 == result) {
                        whetherCheckItem = 1;
                    }
                }

                if (0 == whetherCheckItem) {
                    showToast("点巡检内容要有一个 合格/不合格");
                    return;
                }

                tabJson = JSON.toJSONString(pollingBean);
                confirmType = 1;
                llSbd.setVisibility(View.VISIBLE);
                llCkd.setVisibility(View.GONE);
                tvSbd.setTextColor(getResources().getColor(R.color.color_33));
                tvCkd.setTextColor(getResources().getColor(R.color.color_99));
                return;
            }

            if (2 == jclxID) {
                showToast("请选择检查类型");
                return;
            }

            if (dxjID != isOkId){
                showToast("请至少选择一个点巡检内容 合格/不合格");

                return;
            }
            if (0 == whetherCheckItem) {
                showToast("请至少选择一个点巡检内容 合格/不合格");
                return;
            }
            HttpParams params2 = new HttpParams();
            params2.put("fEqDetailOid", equipID);
            params2.put("isPobigint", "" + dxjID);
            params2.put("CheckType", "" + jclxID);
            params2.put("CheckResult", "" + dxjgID);
            params2.put("fUserCreator", userId);
            params2.put("CheckUser", userName);
            params2.put("checkDetail", tabJson);
            LogUtils.e("shuju=========="+tabJson);
            OkGoUtils.getInstance().postByOkGo(this, URL.INSERT_EQ_CHECK, params2, ResponseBean.class, callback3, 3);
        }
    }

    private Callback callback3 = new Callback<ResponseBean>() {
        @Override
        public void onSuccess(ResponseBean response, int id) {
            if (response.getCode() == 200) {
                showToast("提交成功");
                Intent intent = new Intent();
                setResult(2, intent);
                finish();
            } else {
                showToast(response.getMsg());
            }
        }

        @Override
        public void onError(Throwable e, int id) {
            showToast("提交失败");
        }
    };


    private void initShowTab(TextView one, TextView two, TextView three) {
        if (one != null) {
            one.setBackgroundResource(R.drawable.m_border_13_blue);
            one.setTextColor(getResources().getColor(R.color.blue2));
        }
        if (two != null) {
            two.setBackgroundResource(R.drawable.m_border_13_99);
            two.setTextColor(getResources().getColor(R.color.color_99));
        }
        if (three != null) {
            three.setBackgroundResource(R.drawable.m_border_13_99);
            three.setTextColor(getResources().getColor(R.color.color_99));
        }
    }

}

